﻿using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.GoodsCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class UpdateGoodsCommandHandler : IRequestHandler<UpdateGoodsCommand, APIResult<int>>
    {
        private readonly IBaseRepository<SU_GoodsInfo> goodRep;
        public UpdateGoodsCommandHandler(IBaseRepository<SU_GoodsInfo> goodRep)
        {
            this.goodRep = goodRep;
        }

        public async Task<APIResult<int>> Handle(UpdateGoodsCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var list= goodRep.GetAll().Result.Where(x=>x.GoodsId==request.GoodsId).FirstOrDefault();
            list.GoodsId = request.GoodsId;
            list.GoodNameId=request.GoodNameId;
            list.ProductionCode = request.ProductionCode;
            list.SellByDay = request.SellByDay;
            list.ProNumber = request.ProNumber;
            list.ProductionEnt = request.ProductionEnt;
            list.GoodsPhone = request.GoodsPhone;
            list.UpdateName = request.UpdateName;
            list.UpdateTime = DateTime.Now;
            var res= goodRep.Update(list).Result;
            if (res>0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
                result.data = res;
            }
            return await Task.FromResult(result);
        }
    }
}
